package com.gzsxy.rabbitmq.producer.controller;

import com.alibaba.fastjson.JSON;
import com.gzsxy.rabbitmq.producer.ConstantKey;
import com.gzsxy.rabbitmq.producer.config.redis.StringRedisTemplate;
import com.gzsxy.rabbitmq.producer.dto.TestMessage;
import com.gzsxy.rabbitmq.producer.service.RabbitMQService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Date;
import java.util.UUID;

/**
 * @Description: 消息发送管理模块
 * @Author: xiaolong
 * @date: 2023/2/15 11:30
 * @Version: 1.0
 */
@Slf4j
@RequestMapping("ttl")
@RestController
@Api(tags = "消息发送管理模块")
public class SendMsgController {

    @Autowired
    private RabbitMQService rabbitMQService;

    @Autowired
    private StringRedisTemplate stringRedisTemplate;

    /**
     * 消息发送
     * @param message 消息
     */
    @GetMapping("sendExpirationMsg/{message}")
    @ApiOperation(value = "消息发送")
    public void sendMsg(@PathVariable String message) {
        //消息可靠传输： 把消息发送MQ一份，同时存储到数据库(Redis)一份。开启消息发送确定机制，然后进行消息的状态控制，发送中为0，发送失败为1。
        String id = UUID.randomUUID().toString();
        TestMessage testMessage = new TestMessage();
        testMessage.setExchange("Z");
        testMessage.setRoutingKey("XC");
        testMessage.setMessage(message);
        stringRedisTemplate.opsForHash().put(ConstantKey.sendMsgStatus.SEND.getValue(),id,testMessage);
        rabbitMQService.sendMessageByExchange("Z","XC",testMessage, id);
        log.info("当前时间：{},发送一条信息给队列 E:{}", new Date(), message);
    }
}
